﻿using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ubiety.Dns.Core.Records;
using ZoneLineNew.Common;
using ZoneLineNew.Core;
using ZoneLineNew.Core.EventArgs;
using ZoneLineNew.Enums;
using ZoneLineNew.Models;
using ZoneLineNew.Services.DB;
using ZoneLineNew.ViewModels;

namespace ZoneLineNew.Views {
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : UserControl {

        private readonly IEventAggregator _eventAggregator;

        public MainView(IEventAggregator eventAggregator) {
            InitializeComponent();

            this._eventAggregator = eventAggregator;
            
            _eventAggregator.GetEvent<PubSubEventArgs>().Subscribe(LoggerScroll);
        }


        private void LoggerScroll(MessageData messageData) {
            App.Current?.Dispatcher.Invoke(() => {
                try {
                    var vm = this.DataContext as MainViewModel;
                    if (vm == null)
                    {
                        return;
                    }
                    RichTextBox richTextBox = default;
                    if (messageData.Code == vm.WorkStationOP006Service?.StationConfig?.StationCode)
                    {
                        richTextBox = StationOP006Log;
                    }
                    else if (messageData.Code == vm.WorkStationOP010Service?.StationConfig?.StationCode)
                    {
                        richTextBox = StationOP010Log;
                    }
                    else if (messageData.Code == vm.WorkStationOP020Service?.StationConfig?.StationCode)
                    {
                        richTextBox = StationOP020Log;
                    }
                    else if (messageData.Code == vm.WorkStationOP050LService?.StationConfig?.StationCode)
                    {
                        richTextBox = StationOP050LLog;
                    }
                    else if (messageData.Code == vm.WorkStationOP050RService?.StationConfig?.StationCode)
                    {
                        richTextBox = StationOP050RLog;
                    }
                    else if (messageData.Code == vm.WorkStationOP610Service?.StationConfig?.StationCode)
                    {
                        richTextBox = StationOP610Log;
                    }
                    var msg = messageData.Parameters.GetValue<string>("Message");

                    if (richTextBox?.Document.Blocks.Count > 500) {
                        richTextBox?.Document.Blocks.Clear();
                    }
                    richTextBox?.AppendText(DateTime.Now.ToString("HH:mm:ss") + msg + "\r\n");
                    richTextBox?.ScrollToEnd();

                } catch (TaskCanceledException ex) {
                    LogManager.Error(ex.Message.ToString());
                    //throw;
                }
            });
        }

    }
}
